<?php
/**
 * 发货物流管理
 * Created by PhpStorm.
 * User: g
 * Date: 2018/4/15
 * Time: 10:16
 */

namespace app\index\controller;

use app\index\validate\Valid;
use think\Request;
class Fhwlgl extends Common
{
    /**
     * 发货管理
     */
    public function fhgl(){
        $sn = $this->request->param('sn');
        $ckfs = $this->request->param('ckfs');
        $ckdx_name = $this->request->param('ckdx_name');
        $wllx = $this->request->param('wllx');
        $dyck = $this->request->param('dyck');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $fhzt = $this->request->param('fhzt');
        $this->assign([
            'sn'=>$sn,
            'ckfs'=>$ckfs,
            'ckdx_name'=>$ckdx_name,
            'wllx'=>$wllx,
            'dyck'=>$dyck,
            'start'=>$start,
            'end'=>$end,
            'fhzt'=>$fhzt,
        ]);
        $where = [];
        $where['sfxyfh'] = 1;
        if($fhzt==1){ //所有

        }elseif($fhzt==2){ //已发货的出库单
            $where['id'] = ['exp','in(select ckd_id from cg_fhd)'];
        }else{//待发货
            $where['id'] = ['exp','not in(select ckd_id from cg_fhd)'];
        }
        if($start&&!$end){
            $where['sqrq'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['sqrq'] = ['<',$end];
        }elseif($start&&$end){
            $where['sqrq'] = [['>',$start],['<',$end],'and'];
        }
        if($sn){
            $where['sn'] = ['like','%'.$sn.'%'];
        }
        if($ckfs){
            $where['ckfs'] = $ckfs;
        }
        if($ckdx_name){
            $where['ckdx_name'] = ['like','%'.$ckdx_name.'%'];
        }
        if($wllx){
            $where['wllx'] = $wllx;
        }
        if($dyck){
            $where['dyck'] = $dyck;
        }
        $cnt = db('cg_ckd')->where($where)->count();
        $data = db('cg_ckd')->where($where)->order('id desc')->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
		$maxpage = ceil($cnt/10);
        foreach($data as $k=>$v){
            switch ($v['ckdxlx']){
                case 1: //经销商
                    $v['fhdx'] = db('cg_jxs')->where('id',$v['ckdx'])->value('title');
                    $v['fhdxsn'] = db('cg_jxs')->where('id',$v['ckdx'])->value('sn');
                    break;
                case 2: //网点
                    $v['fhdx'] = db('cg_net')->where('id',$v['ckdx'])->value('title');
                    $v['fhdxsn'] = db('cg_net')->where('id',$v['ckdx'])->value('sn');
                    break;
                case 3: //工厂
                    $v['fhdx'] = '工厂';
                    $v['fhdxsn'] = '';
                    break;
                case 4: //部门
                    $v['fhdx'] = db('department')->where('id',$v['ckdx'])->value('de_name');
                    $v['fhdxsn'] = db('department')->where('id',$v['ckdx'])->value('de_no');
                    break;
                case 5: //个人
                    $v['fhdx'] = db('employee')->where('id',$v['ckdx'])->value('em_name');
                    $v['fhdxsn'] = db('employee')->where('id',$v['ckdx'])->value('em_no');
                    break;
                default: //其他
                    $v['fhdx'] = $v['ckdx'];
                    $v['fhdxsn'] = '';
            }
            $v['sqsl'] = round(db('cg_ckd_list')->where('ckd_id',$v['id'])->sum('num'),4);
            $v['zje'] = round(db('cg_ckd_list')->where('ckd_id',$v['id'])->sum('zje'),2);
            $v['fhd_id'] = db('cg_fhd')->where('ckd_id',$v['id'])->value('id');
            $data[$k] = $v;
        }
        $this->assign([
            'data'=>$data,
            'page'=>$page,
            'maxpage'=>$maxpage,
            'cnt'=>$cnt
        ]);
        //仓库
        $ck = db('cg_ck')->field('id,ckjc')->select();
        $this->assign('ck',$ck);
        return $this->fetch();
    }

    /**
     * 发货
     */
    public function fh(){
        $id = $this->request->param('id');
        if($_POST){
            $data = [];
            $data['bh'] = $this->request->post('bh');
            $data['mc'] = $this->request->post('mc');
            $data['shlxr'] = $this->request->post('shlxr');
            $data['shdh'] = $this->request->post('shdh');
            $data['shdz'] = $this->request->post('shdz');
            $data['rq'] = $this->request->post('rq');
            $data['ssje'] = $this->request->post('ssje');
            $data['zdr'] = $this->request->post('zdr');
            $data['wlgs'] = $this->request->post('wlgs');
            $data['lxr'] = $this->request->post('lxr');
            $data['dh'] = $this->request->post('dh');
            $data['wllx'] = $this->request->post('wllx');
            $data['wldh'] = $this->request->post('wldh');
            $data['ygdhsj'] = $this->request->post('ygdhsj');
            $data['ckd_id'] = $id;
            $data['ckfs'] = db('cg_ckd')->where('id',$id)->value('ckfs');
            $data['dyck'] = db('cg_ckd')->where('id',$id)->value('dyck');
            $data['sn'] = $this->produce_fhd_SN();
            $xssl = $this->request->post('xssl/a');
            $dzsl = $this->request->post('dzsl/a');
            $bz = $this->request->post('bz/a');
            if(in_array('',$xssl)){
                echo json_encode(['success'=>false,'msg'=>'销售数量不能为空']);die;
            }

            if(!$data['wlgs']){
                echo json_encode(['success'=>false,'msg'=>'请选择物流公司']);die;
            }
            //同时减掉出库单里相应的数量
            $ids = $this->request->post('fkdmx_id/a');
            foreach($ids as $k=>$v){
                $num = db('cg_ckd_list')->where('id',$v)->value('num'); //出库数量
                $fhsl = db('cg_ckd_list')->where('id',$v)->value('fhsl'); //已发货数量
                if(($num-$fhsl) < intval($xssl[$k])+intval($dzsl[$k])){
                    echo json_encode(['success'=>false,'msg'=>'某个产品销售数量+搭赠数量超出出库单出库数量']);die;
                }
            }

            $bool = db('cg_fhd')->insertGetId($data);
            if(false!==$bool){

                $data2 = [];
                $i = 1;
                foreach($xssl as $k=>$v){
                    $t = [];
                    $t['pro_id'] = $k;
                    $t['xssl'] = $v;
                    $t['dzsl'] = $dzsl[$k];
                    $t['bz'] = $bz[$k];
                    $t['sn'] = $i;
                    $t['ckdmx_id'] = $ids[$k];
                    $t['fhd_id'] = $bool;
                    $i++;
                    $data2[] = $t;
                }
                db('cg_fhd_list')->insertAll($data2);

                foreach($ids as $k=>$v){
                    db('cg_ckd_list')->where('id',$v)->setInc('fhsl',intval($xssl[$k])+intval($dzsl[$k]));
                }

                $qrck = db('cg_ckd')->where('id',$id)->value('qrck');
                $order_id = db('cg_cksqd t')->join('cg_ckd t2','t2.cksqdbh=t.sn')->where('t2.id',$id)->value('t.order_id');
                $num = db('cg_ckd_list')->where('ckd_id',$id)->value('num');
                $fhsl = db('cg_ckd_list')->where('ckd_id',$id)->value('fhsl');

                if($qrck==1){ //发货完毕
                    //修改对应订单的发货状态
                    db('cg_order')->where(['id'=>$order_id])->update(['fhzt'=>2]);
                }elseif($num>$fhsl){ //部分发货
                    db('cg_order')->where(['id'=>$order_id])->update(['fhzt'=>1]);
                }
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        $data = db('cg_ckd')->where('id',$id)->find();
        $data['dyck'] = db('cg_ck')->where('id',$data['dyck'])->value('ckjc');
        if($data['ckdxlx']==1){
            $data['ckdx_sn'] = db('cg_jxs')->where('id',$data['ckdx'])->value('sn');
            $data['khmc'] = '经销商';
        }elseif($data['ckdxlx']==2){
            $data['ckdx_sn'] = db('cg_net')->where('id',$data['ckdx'])->value('sn');
            $data['khmc'] = '网点';
        }elseif($data['ckdxlx']==3){
            $data['ckdx_sn'] = '';
            $data['khmc'] = '工厂';
        }elseif($data['ckdxlx']==4){
            $data['ckdx_sn'] = db('department')->where('id',$data['ckdx'])->value('de_no');
            $data['khmc'] = '部门';
        }elseif($data['ckdxlx']==5){
            $data['ckdx_sn'] = db('employee')->where('id',$data['ckdx'])->value('em_no');
            $data['khmc'] = '人员';
        }elseif($data['ckdxlx']==6){
            $data['ckdx_sn'] = '';
            $data['khmc'] = '';
        }
        $data['list'] = db('cg_ckd_list')->where(['ckd_id'=>$id,'num'=>['>',0]])->select();
        $has = 0;
        foreach($data['list'] as $k=>$v){
            if($v['sqsl']-$v['num']>0){
                $has = 1;
            }
        }
        $data['yfhsl'] = $has;
        $this->assign('data',$data);
        $ckslhj = db('cg_ckd_list')->where('ckd_id',$id)->sum('num');
        $yfhslhj = db('cg_ckd_list')->where('ckd_id',$id)->sum('fhsl');
        $this->assign('ckslhj',$ckslhj);
        $this->assign('yfhslhj',$yfhslhj);
        $tj = db('cg_ckd_list')->where('ckd_id',$id)->sum('tj');
        $this->assign('tj',$tj);
        $zl = db('cg_ckd_list')->where('ckd_id',$id)->sum('zl');
        $this->assign('zl',$zl);
        $zdr = db('employee')->where('id',session('employee_id'))->value('em_name');
        $this->assign('zdr',$zdr);
        $wlgs = db('cg_wlgs')->select();
        $this->assign('wlgs',$wlgs);

        //生成新的sn
        $fhd_sn = $this->produce_fhd_SN();
        $this->assign('fhd_sn',$fhd_sn);
        return $this->fetch();
    }

    /**
     * 发货单编号
     * @return float|int|string
     */
    public function produce_fhd_SN(){
        //生成编号
        $max = db('cg_fhd')->where('sn','like',date('Ymd').'%')->max('sn'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }

    /**
     * 收货单编号
     * @return float|int|string
     */
    public function produce_shd_SN(){
        //生成编号
        $max = db('cg_shd')->where('sn','like',date('Ymd').'%')->max('sn'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }

    /**
     * 查看出库单
     */
    public function ckckd(){
        $id = $this->request->param('id');
        $data = db('cg_ckd')->where('id',$id)->find();
        $data['dyck'] = db('cg_ck')->where('id',$data['dyck'])->value('ckjc');
        $data['list'] = db('cg_ckd_list')->where('ckd_id',$id)->select();
        $data['ztj'] = db('cg_ckd_list')->where('ckd_id',$id)->sum('tj');
        $data['zzl'] = db('cg_ckd_list')->where('ckd_id',$id)->sum('zl');
        $data['zsqsl'] = db('cg_ckd_list')->where('ckd_id',$id)->sum('sqsl');
        $data['zcksl'] = db('cg_ckd_list')->where('ckd_id',$id)->sum('num');
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 发货单查询
     */
    public function fhdcx(){
        $sn = trim($this->request->param('sn'));
        $ckfs = $this->request->param('ckfs');
        $fhdx = $this->request->param('fhdx');
        $dyck = $this->request->param('dyck');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $qszt = $this->request->param('qszt');
        $wllx = $this->request->param('wllx');
        $bh = $this->request->param('bn');
        $this->assign([
            'sn'=>$sn,
            'ckfs'=>$ckfs,
            'fhdx'=>$fhdx,
            'dyck'=>$dyck,
            'start'=>$start,
            'end'=>$end,
            'qszt'=>$qszt,
            'wllx'=>$wllx,
            'bh'=>$bh,
        ]);
        $where = [];
        if($sn){
            $where['sn'] = ['like','%'.$sn.'%'];
        }
        if($ckfs){
            $where['ckfs'] = $ckfs;
        }
        if($fhdx){
            $where['mc'] = $fhdx;
        }
        if($dyck){
            $where['dyck'] = $dyck;
        }
        if($start&&!$end){
            $where['rq'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['rq'] = ['<',$end];
        }elseif($start&&$end){
            $where['rq'] = [['>',$start],['<',$end],'and'];
        }
        if($qszt){
            $where['qszt'] = $qszt;
        }
        if($wllx){
            $where['wllx'] = $wllx;
        }
        if($bh){
            $where['bh'] = $bh;
        }
        $cnt = db('cg_fhd')->where($where)->count();
        $data = db('cg_fhd')->where($where)->order('id desc')->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
		$maxpage =ceil($cnt/10);
        foreach($data as $k=>$v){
            $xssl = db('cg_fhd_list')->where('fhd_id',$v['id'])->sum('xssl');
            $dzsl = db('cg_fhd_list')->where('fhd_id',$v['id'])->sum('dzsl');
            $v['sl'] = $xssl+$dzsl;
            $v['zdr'] = db('employee')->where('id',$v['zdr'])->value('em_name');
            $v['ckfs'] = db('cg_ckd')->where('id',$v['ckd_id'])->value('ckfs');
            $v['dyck'] = db('cg_ckd')->where('id',$v['ckd_id'])->value('dyck');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'cnt'=>$cnt,
            'maxpage'=>$maxpage
        ]);
        $dycks = db('cg_ck')->select();
        $this->assign('dycks',$dycks);
        return $this->fetch();
    }

    /**
     * 发货单详情
     */
    public function fhdxq(){
        $id = $this->request->param('id');
        $data = db('cg_fhd')->where('id',$id)->find();
        $list = db('cg_fhd_list')->where('fhd_id',$id)->select();
        $cksl = 0;
        $xssl = 0;
        $dzsl = 0;
        $xsje = 0;
        $dzje = 0;
        foreach($list as $k=>$v){

            $v['pro_name'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('title_name');
            $v['unit'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('unit');
            $v['cksl'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('num');
            $v['dj'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('dj');
            $v['tj'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('tj');
            $v['zl'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('zl');
            $cksl += $v['cksl'];
            $xssl += $v['xssl'];
            $dzsl += $v['dzsl'];
            $xsje += $v['xssl']*$v['dj'];
            $dzje += $v['dzsl']*$v['dj'];
            $list[$k] = $v;
        }
        $data['list'] = $list;
        $data['ckd_sn'] = db('cg_ckd')->where('id',$data['ckd_id'])->value('sn');
        $data['dyck'] = db('cg_ck')->where('id',$data['dyck'])->value('ckjc');
        $data['zdr'] = db('employee')->where('id',$data['zdr'])->value('em_name');
        $data['wlgs'] = db('cg_wlgs')->where('id',$data['zdr'])->value('title');
        $this->assign('data',$data);
        $this->assign('cksl',$cksl);
        $this->assign('xssl',$xssl);
        $this->assign('dzsl',$dzsl);
        $this->assign('xsje',$xsje);
        $this->assign('dzje',$dzje);
        return $this->fetch();
    }

    /**
     * 收货管理
     */
    public function shgl(){
        $sn = $this->request->param('sn');
        $ckfs = $this->request->param('ckfs');
        $ckdx_name = $this->request->param('ckdx_name');
        $wllx = $this->request->param('wllx');
        $dyck = $this->request->param('dyck');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $qszt = $this->request->param('qszt');
        $this->assign([
            'sn'=>$sn,
            'ckfs'=>$ckfs,
            'ckdx_name'=>$ckdx_name,
            'wllx'=>$wllx,
            'dyck'=>$dyck,
            'start'=>$start,
            'end'=>$end,
            'qszt'=>$qszt,
        ]);
        $where = [];
        if($qszt==='0'){
            $where['qszt'] = null;
        }elseif($qszt==1){
            $where['qszt'] = 1;
        }
        if($start&&!$end){
            $where['rq'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['rq'] = ['<',$end];
        }elseif($start&&$end){
            $where['rq'] = [['>',$start],['<',$end],'and'];
        }
        if($sn){
            $where['sn'] = ['like','%'.$sn.'%'];
        }
        if($ckfs){
            $where['ckfs'] = $ckfs;
        }
        if($ckdx_name){
            $where['mc'] = ['like','%'.$ckdx_name.'%'];
        }
        if($wllx){
            $where['wllx'] = $wllx;
        }
        if($dyck){
            $where['dyck'] = $dyck;
        }
        $cnt = db('cg_fhd')->where($where)->count();
        $data = db('cg_fhd')->where($where)->order('id desc')->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
		$maxpage = ceil($cnt/10);
        foreach($data as $k=>$v){
            $v['dyck'] = db('cg_ck')->where('id',$v['dyck'])->value('ckjc');
            $v['zdr'] = db('employee')->where('id',$v['zdr'])->value('em_name');
            $v['shd_id'] = db('cg_shd')->where('fhd_id',$v['id'])->value('id');
            $data[$k] = $v;
        }
        $this->assign([
            'data'=>$data,
            'page'=>$page,
            'maxpage'=>$maxpage,
            'cnt'=>$cnt
        ]);
        //仓库
        $ck = db('cg_ck')->field('id,ckjc')->select();
        $this->assign('dycks',$ck);
        return $this->fetch();
    }

    /**
     * 确认收货
     */
    public function qrsh(){
        $id = $this->request->param('id');

        if($_POST){ //确认收货

            $data = [];
            $data['bz'] = $this->request->post('bz');
            $data['shr'] = $this->request->post('shr');
            $data['shrq'] = $this->request->post('shrq');
            $data['estimate'] = $this->request->post('estimate');
            $xssl_ss = $this->request->post('xssl_ss/a');
            $dzsl_ss = $this->request->post('dzsl_ss/a');
            $mxbz = $this->request->post('mxbz/a');
            if(!$data['shr']){
                echo json_encode(['success'=>false,'msg'=>'请填写收货人']);die;
            }
            if(!$data['shrq']){
                echo json_encode(['success'=>false,'msg'=>'请填写日期']);die;
            }
            if(in_array('',$xssl_ss)){
                echo json_encode(['success'=>false,'msg'=>'请填写销售实收数量']);die;
            }
            if(in_array('',$dzsl_ss)){
                echo json_encode(['success'=>false,'msg'=>'请填写搭赠实收数量']);die;
            }
            $data['dyck'] = db('cg_fhd')->where('id',$id)->value('dyck');
            $data['khbh'] = db('cg_fhd')->where('id',$id)->value('bh');
            $data['khmc'] = db('cg_fhd')->where('id',$id)->value('mc');
            $data['shdz'] = db('cg_fhd')->where('id',$id)->value('shdz');
            $data['lxr'] = db('cg_fhd')->where('id',$id)->value('shlxr');
            $data['dh'] = db('cg_fhd')->where('id',$id)->value('shdh');
            $data['fhrq'] = db('cg_fhd')->where('id',$id)->value('rq');
            $data['wlgs'] = db('cg_fhd')->where('id',$id)->value('wlgs');
            $data['wlgs_mc'] = db('cg_wlgs')->where('id',$data['wlgs'])->value('title');
            $data['wlgsdh'] = db('cg_fhd')->where('id',$id)->value('dh');
            $data['wldh'] = db('cg_fhd')->where('id',$id)->value('wldh');
            $data['shr_mc'] = db('employee')->where('id',$data['shr'] )->value('em_name');

            $data['sn'] = $this->produce_shd_SN();
            $data['zdr'] = session('employee_id');
            $data['zdr_mc'] = db('employee')->where('id',$data['zdr'] )->value('em_name');
            $data['wllx'] = db('cg_fhd')->where('id',$id)->value('wllx');
            $data['ckfs'] = db('cg_fhd')->where('id',$id)->value('ckfs');
            $data['fhd_id'] = $id;
            $bool = db('cg_shd')->insertGetId($data);

            if(false!==$bool){
                $ins = [];
                $i = 1;
                foreach($xssl_ss as $k=>$v){
                    $t = [];
                    $t['fhdmx_id'] = $k;
                    $t['pro_id'] = db('cg_fhd_list')->where('id',$k)->value('pro_id');
                    $t['pro_name'] = db('cg_fhd_list t')->join('cg_ckd_list t2','t2.id=t.ckdmx_id')->where('t.id',$k)->value('title_name');
                    $t['unit'] = db('cg_fhd_list t')->join('cg_ckd_list t2','t2.id=t.ckdmx_id')->where('t.id',$k)->value('unit');
                    $t['cksl'] = db('cg_fhd_list t')->join('cg_ckd_list t2','t2.id=t.ckdmx_id')->where('t.id',$k)->value('t2.num');
                    $t['xj'] = db('cg_fhd_list t')->join('cg_ckd_list t2','t2.id=t.ckdmx_id')->where('t.id',$k)->value('dj');

                    $t['xssl_fh'] = db('cg_fhd_list')->where('id',$k)->value('xssl');
                    $t['dzsl_fh'] = db('cg_fhd_list')->where('id',$k)->value('dzsl');
                    $t['xssl_ss'] = $v;
                    $t['dzsl_ss'] = $dzsl_ss[$k];
                    $t['je'] = round($t['xssl_ss']*$t['xj'],2);
                    $t['bz'] = $mxbz[$k];
                    $t['tj'] = db('cg_fhd_list t')->join('cg_ckd_list t2','t2.id=t.ckdmx_id')->where('t.id',$k)->value('tj');
                    $t['zl'] = db('cg_fhd_list t')->join('cg_ckd_list t2','t2.id=t.ckdmx_id')->where('t.id',$k)->value('zl');
                    $t['shd_id'] = $bool;
                    $t['sn'] = $i;
                    $ins[] = $t;
                    $i++;
                }
                db('cg_shd_list')->insertAll($ins);
                db('cg_fhd')->where('id',$id)->update(['qszt'=>1]);
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }

        $data = db('cg_fhd')->where('id',$id)->find();
        $data['dyck'] = db('cg_ck')->where('id',$data['dyck'])->value('ckjc');

        $list = db('cg_fhd_list')->where('fhd_id',$id)->select();
        $cksl = 0;
        $xssl = 0;
        $dzsl = 0;
        $je = 0;
        $tj = 0;
        $zl = 0;
        foreach($list as $k=>$v){
            $v['cpmc'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('title_name');
            $v['unit'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('unit');
            $v['cksl'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('num');
            $v['dj'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('dj');
            $v['tj'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('tj');
            $v['zl'] = db('cg_ckd_list')->where('id',$v['ckdmx_id'])->value('zl');
            $v['je'] = $v['dj']*$v['xssl'];
            $cksl += $v['cksl'];
            $xssl += $v['xssl'];
            $dzsl += $v['dzsl'];
            $je += $v['je'];
            $tj += $v['tj'];
            $zl += $v['zl'];
            $list[$k] = $v;
        }
        $data['list'] = $list;
        $data['wlgs'] = db('cg_wlgs')->where('id',$data['wlgs'])->value('title');
        $this->assign('data',$data);
        $this->assign([
            'cksl'=>$cksl,
            'xssl'=>$xssl,
            'dzsl'=>$dzsl,
            'je'=>$je,
            'tj'=>$tj,
            'zl'=>$zl,
        ]);
        return $this->fetch();
    }
/**
     * 收货单查询
     */
    public function shdcx(){
        $sn = $this->request->param('sn');
        $ckfs = $this->request->param('ckfs');
        $ckdx_name = $this->request->param('khmc');
        $zdr_mc = $this->request->param('zdr_mc');
        $shr_mc = $this->request->param('shr_mc');
        $wllx = $this->request->param('wllx');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $this->assign([
            'sn'=>$sn,
            'ckfs'=>$ckfs,
            'khmc'=>$ckdx_name,
            'wllx'=>$wllx,
            'start'=>$start,
            'end'=>$end,
            'zdr_mc'=>$zdr_mc,
            'shr_mc'=>$shr_mc,
        ]);
        $where = [];
        if($start&&!$end){
            $where['shrq'] = ['>',$start];
        }elseif(!$start&&$end){
            $where['shrq'] = ['<',$end];
        }elseif($start&&$end){
            $where['shrq'] = [['>',$start],['<',$end],'and'];
        }
        if($sn){
            $where['sn'] = ['like','%'.$sn.'%'];
        }
        if($ckfs){
            $where['ckfs'] = $ckfs;
        }
        if($ckdx_name){
            $where['khmc'] = ['like','%'.$ckdx_name.'%'];
        }
        if($zdr_mc){
            $where['zdr_mc'] = ['like','%'.$zdr_mc.'%'];
        }
        if($shr_mc){
            $where['shr_mc'] = ['like','%'.$shr_mc.'%'];
        }
        if($wllx){
            $where['wllx'] = $wllx;
        }
        $cnt = db('cg_shd')->where($where)->count();
        $data = db('cg_shd')->where($where)->order('id desc')->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
		$maxpage = ceil($cnt/10);
        foreach($data as $k=>$v){
            $v['zje'] = db('cg_shd_list')->where('shd_id',$v['id'])->sum('je');
            $data[$k] = $v;
        }
        $this->assign([
            'data'=>$data,
            'page'=>$page,
            'maxpage'=>$maxpage,
            'cnt'=>$cnt
        ]);
        return $this->fetch();
    }

    /**
     * 收货单详情
     */
    public function shdxq(){
        $id = $this->request->param('id');
        $data = db('cg_shd')->where('id',$id)->find();
        $data['list'] = db('cg_shd_list')->where('shd_id',$id)->select();
        $data['ckslhj'] = db('cg_shd_list')->where('shd_id',$id)->sum('cksl');
        $data['xsslhj'] = db('cg_shd_list')->where('shd_id',$id)->sum('xssl_fh');
        $data['dzslhj'] = db('cg_shd_list')->where('shd_id',$id)->sum('dzsl_fh');
        $data['xsslhj2'] = db('cg_shd_list')->where('shd_id',$id)->sum('xssl_ss');
        $data['dzslhj2'] = db('cg_shd_list')->where('shd_id',$id)->sum('dzsl_ss');
        $data['zje'] = db('cg_shd_list')->where('shd_id',$id)->sum('je');
        $data['ztj'] = db('cg_shd_list')->where('shd_id',$id)->sum('tj');
        $data['zzl'] = db('cg_shd_list')->where('shd_id',$id)->sum('zl');
        $data['dyck'] = db('cg_ck')->where('id',$data['dyck'])->value('ckjc');
        $this->assign('data',$data);

        return $this->fetch();
    }
    /**
     * 销售物料发货确认
     */
    public function xswlfhqr(){
        $sn = $this->request->param('sn');
        $title = $this->request->param('title');
        $status = $this->request->param('status');
        $sqr = $this->request->param('sqr');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $where = [];
        if($sn){
            $where['sn'] = ['like','%'.trim($sn).'%'];
        }
        if($title){
            $where['title'] = ['like','%'.trim($title).'%'];
        }
        if($status){
            $where['sqzt'] = $status;
        }
        if($sqr){
            $where['sqr'] = $sqr;
        }
        if($start&&$end){
            $where['sqrq'] = [['>=',$start],['<=',$end],'and'];
        }elseif($start&&!$end){
            $where['sqrq'] = ['>=',$start];
        }elseif(!$start&&$end){
            $where['sqrq'] = ['<=',$end];
        }
        $count = db('cg_xswlff')->where($where)->count();
        $data = db('cg_xswlff')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        $maxpage = ceil($count/10);
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'maxpage'=>$maxpage,
            'count'=>$count
        ]);
        return $this->fetch();
    }

    /**
     * 销售物料发货确认
     */
    public function xswl_shipment(){
        $id = $this->request->param('id');
        if($_POST){

            $bfck = $this->request->post('bfck');
            if($bfck==1){ //部分出库
                $bccksl = $this->request->post('bccksl/a');
                if(in_array('',$bccksl)){
                    echo json_encode(['success'=>false,'msg'=>'请输入出库数量']);die;
                }
                $sqsls = db('cg_xswlff_list')->where(['xswlff_id'=>$id,'qrck'=>0])->field('id,sqsl,ycksl')->select();
                foreach ($sqsls as $k=>$v){
                    if($bccksl[$v['id']]>($v['sqsl']-$v['ycksl'])){
                        echo json_encode(['success'=>false,'msg'=>'出库数量不能大于申请数量']);die;
                    }
                }

                $data = [];
                $data['sn'] = $this->produce_ckd_SN();
                $data['xswlff_id'] = $id;
                $bool = db('cg_xswlff_qrd')->insertGetId($data);
                $ins = [];
                $i = 1;
                $mx = db('cg_cksqd_list')->where('cksqd_id',$id)->select();
                foreach($mx as $k=>$v){
                    $t = [];
                    $t['xswlff_qrd_id'] = $bool;
                    $t['num'] = $v['num'];
                    $t['sn'] = $i;
                    $t['xswlffmx_id'] = $k;
                    $ins[] = $t;
                    $i++;
                }
                db('cg_xswlff_qrd_list')->insertAll($ins);

                $qrck = $this->request->post('qrck');
                if($qrck){ //全部出库完毕
                    $qtyy = $this->request->post('qtyy');
                    db('cg_xswlff')->where('id',$id)->update(['qrck'=>1,'qtyy'=>$qtyy]);
                    db('cg_xswlff_list')->where('xswlff_id',$id)->update(['qrck'=>1]);
                }

                //修改确认人
                db('cg_xswlff')->where('id',$id)->update([
                    'qrr'=>session('employee_id'),
                    'qrrbm'=>db('department t')->join('department_employee t2','t.id=t2.department_id')->where(['t2.employee_id'=>session('employee_id')])->value('de_name'),
                    'qrrq'=>date('Y-m-d')
                ]);
                echo json_encode(['success'=>true,'msg'=>'出库成功']);
            }elseif ($bfck==2) { //出库完毕
                $bccksl = $this->request->post('bccksl/a');
                if(in_array('',$bccksl)){
                    echo json_encode(['success'=>false,'msg'=>'请输入出库数量']);die;
                }
                $sqsls = db('cg_xswlff_list')->where(['xswlff_id'=>$id,'qrck'=>0])->field('id,sqsl')->select();
                foreach ($sqsls as $k=>$v){
                    if($bccksl[$v['id']]>($v['sqsl']-$v['ycksl'])){
                        echo json_encode(['success'=>false,'msg'=>'出库数量不能大于申请数量']);die;
                    }
                }

                $data = [];
                $data['sn'] = $this->produce_ckd_SN();
                $data['xswlff_id'] = $id;
                $bool = db('cg_xswlff_qrd')->insertGetId($data);
                $ins = [];
                $i = 1;
                $mx = db('cg_xswlff_list')->where('cksqd_id',$id)->select();
                foreach($mx as $k=>$v){
                    $t = [];
                    $t['xswlff_qrd_id'] = $bool;
                    $t['num'] = $v['sqsl']-$v['ycksl'];
                    $t['sn'] = $i;
                    $t['xswlffmx_id'] = $v['id'];
                    $ins[] = $t;
                    $i++;
                }
                db('cg_xswlff_qrd_list')->insertAll($ins);

                //修改确认人
                db('cg_xswlff')->where('id',$id)->update([
                    'qrr'=>session('employee_id'),
                    'qrrbm'=>db('department t')->join('department_employee t2','t.id=t2.department_id')->where(['t2.employee_id'=>session('employee_id')])->value('de_name'),
                    'qrrq'=>date('Y-m-d')
                ]);

                //原申请单出库完毕
                db('cg_xswlff')->where('id',$id)->update(['qrck'=>1]);
                db('cg_xswlff_list')->where('id',$id)->update(['qrck'=>1,'ycksl'=>['exp','=sqsl']]);
                echo json_encode(['success'=>true,'msg'=>'出库成功']);
            }

            die;
        }
        $data = db('cg_xswlff')->where('id',$id)->find();
        $data['list'] = db('cg_xswlff_list')->where('xswlff_id',$id)->select();
        $data['sqsl_xj'] = db('cg_xswlff_list')->where('xswlff_id',$id)->sum('sqsl');
        $data['ycksl_xj'] = db('cg_xswlff_list')->where('xswlff_id',$id)->sum('ycksl');
        $data['tj_xj'] = db('cg_xswlff_list')->where('xswlff_id',$id)->sum('tj');
        $data['zl_xj'] = db('cg_xswlff_list')->where('xswlff_id',$id)->sum('zl');
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 出库单
     * @return float|int|string
     */
    public function produce_ckd_SN(){
        //生成编号
        $max = db('cg_xswlff_qrd')->where('sn','like',date('Ymd').'%')->max('cast(sn as SIGNED INTEGER)'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }
}